package com.scanner;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

@SuppressLint("NewApi")
public class ContourView extends View {

		private FrameContour contour;
		private Paint contourPaint = new Paint();
		private final int pointThickness = 15;
		private TurnView turnView;
		private TiltView tiltView;
		
		private int displayHeight;
		private int displayWidth;
		
		private boolean clearFlag = false;
		
		//events handling
	    ArrayList<ScannerEventListener> scannerToggleEventObservers = new ArrayList<ScannerEventListener>();
	    
	    public void SetEventObserver(ScannerEventListener observer){
	    	scannerToggleEventObservers.add(observer);
	    }
	    
	    public void RaiseToggleScannerEvent(boolean set, String method)
	    {
	    	for (ScannerEventListener eventListner: scannerToggleEventObservers)
	    	{
	    		eventListner.ScannerEventCallback(set, method);
	    	}
	    }
	    ///
		
		public ContourView(Context context, FrameContour contourParam)
		{
			super(context);
			contour = contourParam;
			
			contourPaint.setStrokeWidth(8);
			contourPaint.setARGB(120, 250, 250, 250);
			setWillNotDraw(false);
			
			tiltView = new TiltView(context);
			turnView = new TurnView(context);
			
			WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		    Display display = wm.getDefaultDisplay();
		    Point screenSize = new Point();
		    display.getSize(screenSize);
		    displayHeight = screenSize.y;
		    displayWidth = screenSize.x;
		}
		

	    private void UpdatePosition()
	    {	    	
	    	tiltView.Clear();
	    	turnView.Clear();
	    	
	    	//checking turn
	    	MoveActions turnAction = contour.TurnState();
	    	turnView.UpdatePosition(turnAction);
	    	if (turnAction != MoveActions.DONT_TURN)
	    	{
	    		RaiseToggleScannerEvent(false,"");
	    		return;
	    	}
	    	
	    	//checking move
	    	MoveActions moveAction = contour.MoveState(displayHeight, displayWidth);
	    	tiltView.UpdatePosition(moveAction);
	    	
	    	if (moveAction != MoveActions.DONT_MOVE)
	    	{
	    		RaiseToggleScannerEvent(false,"");
	    		return;
	    	}
	    	
	    	//checking tilt
	    	MoveActions tiltAction = contour.TiltState();
	    	tiltView.UpdatePosition(tiltAction);
	    	
	    	if (tiltAction != MoveActions.DONT_TILT)
	    	{
	    		RaiseToggleScannerEvent(false,"");
	    		return;
	    	}
	    		    	
	    	//checking zoom
	    	
	    	RaiseToggleScannerEvent(true,"");
	    }
		
		@Override
		public void onDraw(Canvas canvas)
		{
			if (contour == null || contour.Started() == false)
			{
				return;
			}
			
			if (clearFlag != true)
			{
				canvas.drawCircle((float)contour.topLeft.x, (float)contour.topLeft.y, pointThickness, contourPaint);			
				canvas.drawCircle((float)contour.topRight.x, (float)contour.topRight.y, pointThickness, contourPaint);
				canvas.drawCircle((float)contour.bottomLeft.x, (float)contour.bottomLeft.y, pointThickness, contourPaint);
				canvas.drawCircle((float)contour.bottomRight.x, (float)contour.bottomRight.y, pointThickness, contourPaint);
				
				canvas.drawLine((float)contour.topLeft.x, (float)contour.topLeft.y,(float)contour.topRight.x, (float)contour.topRight.y, contourPaint);
				canvas.drawLine((float)contour.topLeft.x, (float)contour.topLeft.y,(float)contour.bottomLeft.x, (float)contour.bottomLeft.y, contourPaint);
				canvas.drawLine((float)contour.bottomLeft.x, (float)contour.bottomLeft.y,(float)contour.bottomRight.x, (float)contour.bottomRight.y, contourPaint);
				canvas.drawLine((float)contour.topRight.x, (float)contour.topRight.y,(float)contour.bottomRight.x, (float)contour.bottomRight.y, contourPaint);
				
				tiltView.draw(canvas);
				turnView.draw(canvas);
			}
			else
			{
				clearFlag = false;
			}
		}
		
		public void UpdateContour(Point tL, Point tR, Point bR, Point bL)
		{
			contour.UpdatePoints(tL, tR, bR, bL);
			UpdatePosition();
		}
		
		public void ClearView()
		{
			clearFlag = true;
		}
}
